<template>
    <div style="width: 300px;height: 300px;border: 1px solid #ccc;float: right;margin-top: 400px;">
        <h2>登录页面</h2>
        <el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" :rules="rules" label-width="auto">
            <el-form-item label="请输入用户名" prop="username">
                <el-input v-model="ruleForm.username" />
            </el-form-item>
            <el-form-item label="请输入密码" prop="password">
                <el-input v-model="ruleForm.password" />
            </el-form-item>
            <el-button type="primary" @click="submitForm(ruleFormRef)">
                登录
            </el-button>
        </el-form>
    </div>
</template>

<script setup>
import request from '@/request/request'
import router from '@/router'
import { reactive, ref } from 'vue'
const ruleFormRef = ref()
const ruleForm = reactive({
    username: '',
    password: ''
})
const rules = reactive({
    username: [
        { required: true, message: '请输入账号', trigger: 'blur' },
        { min: 3, max: 9, message: '账号长度3-9', trigger: 'blur' },
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 3, max: 9, message: '密码长度3-9', trigger: 'blur' },
    ],
})
const submitForm = async (formEl) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            // console.log('submit!')
            request.post('/login', ruleForm).then(res => {
                console.log('登录', res);
                if (res.data.code == 10000) {
                    ElMessage({
                        message: '登录成功',
                        type: 'success',
                    })
                    router.push('/home')
                }
                else {
                    ElMessage.error('登录失败')
                }
            })
        } else {
            console.log('error submit!', fields)
        }
    })
}
</script>

<style lang="scss" scoped></style>